import { defHttp } from '@/utils/http/axios';
import {
  getMenuListResultModel,
  GetUserInfoModel,
  LoginParams,
  LoginResultModel,
  UploadApiResult,
} from './model';

import { ErrorMessageMode, UploadFileParams } from '#/axios';
import { AxiosProgressEvent } from 'axios';

enum Api {
  Login = '/basic-api/v2/admin/login',
  Logout = '/basic-api/v2/admin/logout',
  //后台菜单
  AdminInfo = '/basic-api/v2/admin/info',
  AdminMenuList = '/basic-api/v2/admin/menuPermission',
  //sys
  UploadImgUrl = '/basic-api/v2/admin/uploadImg',
  UploadUrl = '/basic-api/v2/admin/upload',
  TestRetry = '/mock-api/testRetry',
}

//登录和退出
export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') {
  return defHttp.post<LoginResultModel>({ url: Api.Login, params }, { errorMessageMode: mode });
}

export function getUserInfo() {
  return defHttp.post<GetUserInfoModel>({ url: Api.AdminInfo }, { errorMessageMode: 'none' });
}

export const getMenuList = () => {
  return defHttp.post<getMenuListResultModel>({ url: Api.AdminMenuList });
};

export function doLogout() {
  return defHttp.post({ url: Api.Logout });
}

export function testRetry() {
  return defHttp.post(
    { url: Api.TestRetry },
    {
      retryRequest: {
        isOpenRetry: true,
        count: 5,
        waitTime: 1000,
      },
    },
  );
}

// 公共接口
export function uploadImgApi(
  params: UploadFileParams,
  onUploadProgress: (progressEvent: AxiosProgressEvent) => void,
) {
  return defHttp.uploadFile<UploadApiResult>(
    {
      url: Api.UploadImgUrl,
      onUploadProgress,
    },
    params,
  );
}

export function uploadGoodsImgApi(
  params: UploadFileParams,
  onUploadProgress: (progressEvent: AxiosProgressEvent) => void,
) {
  const ApiUrl = Api.UploadImgUrl;
  return defHttp.uploadFile<UploadApiResult>(
    {
      url: ApiUrl,
      onUploadProgress,
    },
    params,
  );
}

export function uploadFileApi(
  params: UploadFileParams,
  onUploadProgress: (progressEvent: AxiosProgressEvent) => void,
) {
  return defHttp.uploadFile<UploadApiResult>(
    {
      url: Api.UploadUrl,
      onUploadProgress,
    },
    params,
  );
}
